﻿Public Class Form1

  '  Public WithEvents documento _
  '      As New System.Drawing.Printing.PrintDocument

  Private Sub Button1_Click(
      ByVal sender As System.Object,
      ByVal e As System.EventArgs) _
      Handles Button1.Click
    OpenFileDialog1.InitialDirectory = _
       Me.TextBox2.Text
    OpenFileDialog1.Filter =
       "file di testo (*.txt)|*.txt|" &
       "tutti i file (*.*)|*.*"
    OpenFileDialog1.FilterIndex = 1
    OpenFileDialog1.RestoreDirectory = True
    If OpenFileDialog1.ShowDialog() =
        Windows.Forms.DialogResult.OK Then
      ' codice per leggere il file
      Me.TextBox1.Text =
        My.Computer.FileSystem.ReadAllText("" &
        OpenFileDialog1.FileName)
    End If
  End Sub

  Private Sub Button2_Click(
      ByVal sender As System.Object,
      ByVal e As System.EventArgs) _
      Handles Button2.Click
    Me.TextBox1.Text = ""
  End Sub

  Private Sub Button3_Click(
      ByVal sender As System.Object,
      ByVal e As System.EventArgs) _
      Handles Button3.Click
    SaveFileDialog1.InitialDirectory = _
       Me.TextBox2.Text
    SaveFileDialog1.Filter =
       "file di testo (*.txt)|*.txt|" &
       "tutti i file (*.*)|*.*"
    SaveFileDialog1.FilterIndex = 2
    SaveFileDialog1.RestoreDirectory = True
    If SaveFileDialog1.ShowDialog() =
        Windows.Forms.DialogResult.OK Then
      My.Computer.FileSystem.WriteAllText("" &
      SaveFileDialog1.FileName,
      Me.TextBox1.Text, False)
    End If
  End Sub

  Private Sub Button4_Click(
      ByVal sender As System.Object,
      ByVal e As System.EventArgs) _
      Handles Button4.Click
    FolderBrowserDialog1.SelectedPath =
       Me.TextBox2.Text
    If FolderBrowserDialog1.ShowDialog() =
          Windows.Forms.DialogResult.OK Then
      Me.TextBox2.Text =
         FolderBrowserDialog1.SelectedPath
    End If
  End Sub

  Private Sub Form1_Load(ByVal sender _
        As System.Object, _
        ByVal e As System.EventArgs)
    ' impostazione del font:
    Dim f As New Font("Arial", 12)
    Me.TextBox1.Font = f
    Me.TextBox2.Font = f
    ' istruzioni per la stampa:
    documento =
      New System.Drawing.Printing.PrintDocument
    InizializzaPrintPreviewDialog()
  End Sub

  Private Sub Button5_Click(
      ByVal sender As System.Object,
      ByVal e As System.EventArgs) _
      Handles Button5.Click
    FontDialog1.ShowColor = True
    FontDialog1.Font = TextBox1.Font
    FontDialog1.Color = TextBox1.ForeColor
    If FontDialog1.ShowDialog() <> _
          Windows.Forms.DialogResult.Cancel Then
      TextBox1.Font = FontDialog1.Font
      TextBox2.Font = FontDialog1.Font
      TextBox1.ForeColor = FontDialog1.Color
      TextBox2.ForeColor = FontDialog1.Color
    End If
  End Sub

  Private Sub Button6_Click(
      ByVal sender As System.Object,
      ByVal e As System.EventArgs) _
      Handles Button6.Click
    ColorDialog1.Color = TextBox1.BackColor
    ' modifica il colore del testo di TextBox1
    ' con il colore selezionato:
    If (ColorDialog1.ShowDialog() =
          Windows.Forms.DialogResult.OK) Then
      TextBox1.ForeColor = ColorDialog1.Color
    End If
  End Sub

  Private Sub Button7_Click(
      ByVal sender As System.Object,
      ByVal e As System.EventArgs) _
      Handles Button7.Click
    ' inizializza il controllo con le impostazioni
    ' attuali della stampante predefinita
    PageSetupDialog1.PageSettings =
    New System.Drawing.Printing.PageSettings
    PageSetupDialog1.PrinterSettings =
      New System.Drawing.Printing.PrinterSettings
    ' apre la finestra di dialogo:
    PageSetupDialog1.ShowDialog()
  End Sub

  Private Sub Button8_Click(
      ByVal sender As System.Object,
      ByVal e As System.EventArgs) _
      Handles Button8.Click
    ' imposta la proprietà
    ' PrintPreviewDialog.Document all'oggetto
    ' PrintDocument selezionato dall'utente
    PrintPreviewDialog1.Document = documento
    ' chiama il metodo ShowDialog che scatenerà
    ' l'evento PrintPage del documento
    PrintPreviewDialog1.ShowDialog()
  End Sub

  ' inizializza il controllo
  Private Sub InizializzaPrintPreviewDialog()
    ' imposta dimensione, posizione e nome
    Me.PrintPreviewDialog1.ClientSize =
      New System.Drawing.Size(400, 300)
    Me.PrintPreviewDialog1.Location = _
      New System.Drawing.Point(29, 29)
    Me.PrintPreviewDialog1.Name =
      "PrintPreviewDialog1"
    ' imposta la dimensione minima della finestra
    ' di dialogo
    Me.PrintPreviewDialog1.MinimumSize =
      New System.Drawing.Size(375, 250)
    ' imposta la proprietà UseAntiAlias a True
    ' per permettere al sistema operativo di
    ' utilizzare l'effetto antialiasing
    Me.PrintPreviewDialog1.UseAntiAlias = True
  End Sub

  Private Sub document_PrintPage(
      ByVal sender As Object,
      ByVal e As  _
      System.Drawing.Printing.PrintPageEventArgs) _
      Handles documento.PrintPage
    ' Il seguente codice prepara un semplice
    ' messaggio sul documento creato nella
    ' finestra di dialogo
    Dim testo As String = Environment.NewLine &
      "Sito dell'autore: www.deghetto.it"
    Dim printFont As _
    New System.Drawing.Font("Arial", 25,
      System.Drawing.FontStyle.Regular)
    e.Graphics.DrawString(testo, printFont,
      System.Drawing.Brushes.Black, 0, 0)
    testo = Me.TextBox1.Text
    printFont =
      New System.Drawing.Font("Arial", 10,
        System.Drawing.FontStyle.Regular)
    Dim rettangolo As _
    New RectangleF(0, 100, 800, 1100)
    e.Graphics.DrawString(testo, printFont,
      System.Drawing.Brushes.Black, rettangolo)
  End Sub

  Private Sub Button9_Click(
      ByVal sender As System.Object,
      ByVal e As System.EventArgs) _
      Handles Button9.Click
    ' permette all'utente di selezionare l'intervallo
    ' delle pagine da stampare
    PrintDialog1.AllowSomePages = True
    ' imposta la proprietà Document a PrintDocument
    PrintDialog1.Document = documento
    ' se l'utente preme il pulsante OK 
    ' viene stampato il documento
    If (PrintDialog1.ShowDialog() =
        Windows.Forms.DialogResult.OK) Then
      documento.Print()
    End If
  End Sub
End Class
